<% layout('/layouts/default.html', {title: '商品选择', libs: ['dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-body">
			<#form:form id="searchForm" model="${goods}" action="${ctx}/storage/goods/listData" method="post" class="form-inline "
			data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
			<#form:hidden name="isAll" value="${parameter.isAll}"/>
			<div class="form-group">
				<label class="control-label">${text('品牌店铺')}：</label>
				<div class="control-inline" style="width: 180px;">
					<#form:select path="brandId" class="form-control" items="${brands}" itemLabel="name" itemValue="id" blankOption="true" blankOptionLabel="全部品牌"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">${text('商品分类')}：</label>
				<div class="control-inline" style="width: 180px;">
					<#form:select path="cateId" class="form-control" items="${categoies}" itemLabel="name" itemValue="id" blankOption="true" blankOptionLabel="全部分类"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">${text('商品编码')}：</label>
				<div class="control-inline">
					<#form:input path="code" maxlength="255" class="form-control width-120"/>
				</div>
			</div>
			<div class="form-group">
				<label class="control-label">${text('商品名称')}：</label>
				<div class="control-inline">
					<#form:input path="name" maxlength="255" class="form-control width-120"/>
				</div>
			</div>
			<div class="form-group">
				<button type="submit" class="btn btn-primary btn-sm">查询</button>
				<button type="reset" class="btn btn-default btn-sm">重置</button>
			</div>
		</#form:form>
		<div class="row">
			<div class="col-xs-10 pr10">
				<table id="dataGrid"></table>
				<div id="dataGridPage"></div>
			</div>
			<div class="col-xs-2 pl0">
				<div id="selectData" class="tags-input"></div>
			</div>
		</div>
	</div>
</div>
</div>
<% } %>
<script>
	var selectData = ${isNotBlank(selectData!) ? selectData! : "{\}"},
	selectNum = 0, dataGrid = $('#dataGrid').dataGrid({
		searchForm: $("#searchForm"),
		columnModel: [
			{header:'${text("品牌店铺")}', name:'brand', index:'a.brand', width:150, align:"center",formatter: function(val, obj, row, act){
					return row.brand ? row.brand.name : "";
				}},
			{header:'${text("商品分类")}', name:'cate', index:'a.cate', width:150, align:"center",formatter: function(val, obj, row, act){
					return row.cate ? row.cate.name : "";
				}},
			{header:'${text("商品主图")}', name:'thumbnail', index:'a.thumbnail', width:110, align:"center",formatter: function(val, obj, row, act){
					return '<img src="' + val + '" style="width: 80px;height: 80px"/>';
				}},
			{header:'${text("商品名称")}', name:'name', index:'a.name', align:"left"},
			{header:'${text("吊牌价")}', name:'price', index:'a.price', width:90, align:"center",formatter: function(val, obj, row, act){
					return '￥' + val;
				}},
			{header:'行数据', name:'rowData', hidden:true, formatter: function(val, obj, row, act){
					return JSON.stringify(row);
				}}
		],
		autoGridHeight: function(){
			var height = $(window).height() - $('#searchForm').height() - $('#dataGridPage').height() - 75;
			$('.tags-input').height($('.ui-jqgrid').height() - 10);
			return height;
		},
		showCheckbox: '${parameter.checkbox}' == 'true',
		multiboxonly: false, // 单击复选框时再多选
		ajaxSuccess: function(data){
			$.each(selectData, function(key, value){
				dataGrid.dataGrid('setSelectRow', key);
			});
			initSelectTag();
		},
		onSelectRow: function(id, isSelect, event){
			if ('${parameter.checkbox}' == 'true'){
				if(isSelect){
					selectData[id] = JSON.parse(dataGrid.dataGrid('getRowData', id).rowData);
				}else{
					delete selectData[id];
				}
			}else{
				selectData = {};
				selectData[id] = JSON.parse(dataGrid.dataGrid('getRowData', id).rowData);
			}
			initSelectTag();
		},
		onSelectAll: function(ids, isSelect){
			if ('${parameter.checkbox}' == 'true'){
				for (var i=0; i<ids.length; i++){
					if(isSelect){
						selectData[ids[i]] = JSON.parse(dataGrid.dataGrid('getRowData', ids[i]).rowData);
					}else{
						delete selectData[ids[i]];
					}
				}
			}
			initSelectTag();
		},
		ondblClickRow: function(id, rownum, colnum, event){
			if ('${parameter.checkbox}' != 'true'){
				js.layer.$('#' + window.name).closest('.layui-layer')
						.find(".layui-layer-btn0").trigger("click");
			}
			initSelectTag();
		}
	});
	function initSelectTag(){
		selectNum = 0;
		var html = [];
		$.each(selectData, function(key, value){
			selectNum ++;
			html.push('<span class="tag" id="'+key+'_tags-input"><span>'+value.name+' </span>'
					+ '<a href="#" onclick="removeSelectTag(\''+key+'\');" title="取消选择">x</a></span>');
		});
		html.unshift('<div class="title">当前已选择<span id="selectNum">'+selectNum+'</span>项：</div>');
		$('#selectData').empty().append(html.join(''));
	}
	function removeSelectTag(key){
		delete selectData[key];
		dataGrid.dataGrid('resetSelection', key);
		$('#selectNum').html(--selectNum);
		$('#'+key+'_tags-input').remove();
	}
	function getSelectData(){
		return selectData;
	}
</script>
